<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Javascript tests</title>
        <link rel="stylesheet" href="../../../superlists/static/tests/qunit.css">
    </head>
    <body>
        <div id="qunit"></div>
        <div id="qunit-fixture">
            <a id="id_login">Sign in</a>
        </div>
        <script src="../../../superlists/static/tests/jquery-1.10.2.min.js"></script>
        <script src="../../../superlists/static/tests/qunit.js"></script>
        <script src="../../../superlists/static/tests/sinon-1.17.6.js"></script>
        <script src="../accounts.js"></script>
        <script>
            /*global $, test, equal, sinon, Superlists */
            var user, token, urls, mockNavigator, requests, xhr;
            module("navigator.id.watch tests", {
                setup: function () {
                    user = 'current user';
                    token = 'csrf token';
                    urls = { login: 'login url', logout: 'logout url' };
                    mockNavigator = {
                        id: {
                            watch: sinon.mock()
                        }
                    };
                    xhr = sinon.useFakeXMLHttpRequest();
                    requests = [];
                    xhr.onCreate = function (request) { requests.push(request); };
                },
                teardown: function () {
                    mockNavigator.id.watch.reset();
                    xhr.restore();
                }
            });
            test("initialize calls navigator.id.watch", function () {
                Superlists.Accounts.initialize(mockNavigator, user, token, urls);
                equal(mockNavigator.id.watch.calledOnce, true, 'check watch function called');
            });
            test("watch sees current user", function () {
                Superlists.Accounts.initialize(mockNavigator, user, token, urls);
                var watchCallArgs = mockNavigator.id.watch.firstCall.args[0];
                equal(watchCallArgs.loggedInUser, user, 'check user');
            });
            test("initialize binds sign in button to navigator.id.request", function () {
                var requestWasCalled = false;
                var mockRequestFunction = function () { requestWasCalled = true; };
                var mockNavigator = {
                    id: {
                        request: mockRequestFunction,
                        watch: function () {}
                        //doSomethingElse: function () { console.log("called me!");}
                    }
                };
                Superlists.Accounts.initialize(mockNavigator);
                $('#id_login').trigger('click');
                equal(requestWasCalled, true);
            });
            test("initialize calls navigator.id.watch", function () {
                var user = 'current user';
                var token = 'csrf token';
                var urls = {login: 'login url', logout: 'logout url'};
                var mockNavigator = {
                    id: {
                        watch: sinon.mock()
                    }
                };
                Superlists.Accounts.initialize(mockNavigator, user, token, urls);
                equal(mockNavigator.id.watch.calledOnce, true, 'check watch function called');
            });
            test("watch sees current user", function () {
                var user = 'current user';
                var token = 'csrf token';
                var urls = {login: 'login url', logout: 'logout url'};
                var mockNavigator = {
                    id: {
                        watch: sinon.mock()
                    }
                };
                Superlists.Accounts.initialize(mockNavigator, user, token, urls);
                var watchCallArgs = mockNavigator.id.watch.firstCall.args[0];
                equal(watchCallArgs.loggedInUser, user, 'check user');
            });
            test("onlogin does ajax post to login url", function () {
                Superlists.Accounts.initialize(mockNavigator, user, token, urls);
                var onloginCallback = mockNavigator.id.watch.firstCall.args[0].onlogin;
                onloginCallback();
                equal(requests.length, 1, 'check ajax request');
                equal(requests[0].method, 'POST');
                equal(requests[0].url, urls.login, 'check url');
            });
            test("onlogin sends assertion with csrf token", function () {
                Superlists.Accounts.initialize(mockNavigator, user, token, urls);
                var onloginCallback = mockNavigator.id.watch.firstCall.args[0].onlogin;
                var assertion = 'browser-id assertion';
                onloginCallback(assertion);
                equal(
                    requests[0].requestBody,
                    $.param({ assertion: assertion, csrfmiddlewaretoken: token }), //➍
                    'check POST data'
                );
            });
            test("onlogout is just a placeholder", function () {
                Superlists.Accounts.initialize(mockNavigator, user, token, urls);
                var onlogoutCallback = mockNavigator.id.watch.firstCall.args[0].onlogout;
                equal(typeof onlogoutCallback, "function", "onlogout should be a function");
            });
            test("onlogin post failure should do navigator.id.logout ", function () {
                mockNavigator.id.logout = sinon.mock();
                Superlists.Accounts.initialize(mockNavigator, user, token, urls);
                var onloginCallback = mockNavigator.id.watch.firstCall.args[0].onlogin;
                var server = sinon.fakeServer.create();
                server.respondWith([403, {}, "permission denied"]);
                onloginCallback();
                equal(mockNavigator.id.logout.called, false, 'should not logout yet');
                server.respond();
                equal(mockNavigator.id.logout.called, true, 'should call logout');
            });
        </script>
    </body>
</html>